package com.chijun.mform.field.impl;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.chijun.mform.field.UpLoadFileListener;
import com.chijun.mform.full.mode.ModeFormFile;
import com.chijun.mform.full.mode.ModeFormFullFileData;
import com.example.ccj.b.c.custom.file.FileAdapterListener;
import com.example.ccj.b.c.custom.file.FileMode;
import com.example.ccj.b.c.custom.file.FileNetworkAddListener;
import com.example.ccj.b.c.custom.file.FileNetworkDeleteListener;
import com.example.ccj.b.c.custom.file.FilePickControl;
import com.example.ccj.b.c.dialog.LoadingDialog;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ToastUtils;
import com.example.ccj.u.sqlite.SqliteExcuteCommand;
import com.example.ccj.u.tengxun.cos.COS;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.ObservableEmitter;

/**
 * 附件
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:43
 */
public class FieldFor12FuJian extends BaseField {
    List<File> fileModes;
    public FieldFor12FuJian(){
        super();
        formFileModeList = new ArrayList<>();
        fileModes = new ArrayList<>();
    }

    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;

        writeFormClass.fieldFullContentView.setVisibility(View.GONE);
        writeFormClass.fieldFullNameView.setVisibility(View.GONE);

        FilePickControl filePickControl = new FilePickControl(context);
        filePickControl.getTitleBarOnDisplayControl().getTvTitleView().setTextSize(writeFormClass.titleTextSize);
        filePickControl.getTitleBarOnDisplayControl().getTvLeftTag().setBackgroundColor(writeFormClass.leftColor);

        //加载暂存文件
        List<ModeFormFile> formFileModeList1 = SqliteExcuteCommand.get(context, ModeFormFile.class, "sanJiId=?", String.valueOf(formField.getId()));
        formFileModeList.addAll(formFileModeList1);
        for (ModeFormFile formFileMode : formFileModeList1) {
            File file = new File(formFileMode.path);
            fileModes.add(file);
        }

        filePickControl.setLoadMode(FilePickControl.NETWORK_AND_LOCAL_UPLOAD)
                .setMaxSum(writeFormClass.formField.getLittleNum())
                .setTagText(writeFormClass.formField.getHint())
                .setTitleName(writeFormClass.formField.getName())
                .setFileList(fileModes)
                .setFileAdapterListener(new FileAdapterListener() {
                    @Override
                    public void onDelete(FileMode fileMode, FileNetworkDeleteListener fileNetworkDeleteListener) {
                        LoadingDialog loadingDialog = new LoadingDialog(context);
                        loadingDialog.showLoading();

                        ModeFormFile formFileMode = formFileModeMap.get(fileMode.file.getName());
                        if (formFileMode != null) {
                            functionDeleteImg(context, formFileMode.url, new CosFileDeleteListener() {
                                @Override
                                public void onError(String error) {
                                    loadingDialog.dismiss();
                                    ToastUtils.showShort(error);
                                }
                                @Override
                                public void onSuccess(String path) {
                                    fileNetworkDeleteListener.onComplete(fileMode); //回调删除显示的文件
                                    loadingDialog.dismiss();
                                    LogUtils.d("删除成功：" + formFileMode.url);
                                }
                                @Override
                                public void onSuccess(ModeFormFile formFileMode) {

                                }
                            });
                        }
                    }

                    @Override
                    public void onAdd(FileMode fileMode, FileNetworkAddListener fileNetworkChangeListener) {
                        writeFormClass.functionSaveFile(context, fileMode.file, (formImageMode, file, localMedia) -> {
                            formFileModeList.add(formImageMode);
                            fileNetworkChangeListener.onComplete(fileMode);
                        });
                    }
                }).builder();

        writeFormClass.fieldFullContentLayout.addView(filePickControl);

        return writeFormClass.view;
    }


    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.goneFieldNameView();
        //隐藏值字段
        fullModel.goneContentView();

        FilePickControl filePickControl = new FilePickControl(context);
        filePickControl.getTitleBarOnDisplayControl().getTvTitleView().setTextSize(fullModel.localConfig.field_name_text_size);
        filePickControl.getTitleBarOnDisplayControl().getTvLeftTag().setBackgroundColor(fullModel.localConfig.leftColor);

        filePickControl.setTitleName(fullModel.formField.getName());
//        filePickControl.setTagText(fullModel.formField.getHint());
        filePickControl.setMaxSum(fullModel.formField.getLittleNum());

        //该审批人有修改权限时加载模式
        if (fullModel.isHaveModifyPower()) {
            // TODO: 2024/12/26 这个加载模式代表了是否可以修改图片 先加载后上传
            filePickControl.setLoadMode(FilePickControl.NETWORK_AND_LOCAL_NO_UPLOAD);
            filePickControl.getTitleBarOnDisplayControl().getTvLeftTag().setBackgroundColor(fullModel.localConfig.field_have_modify_power_left_tag_color);
        }
        //该审批人无修改权限时加载模式
        else filePickControl.setLoadMode(FilePickControl.NETWORK);
        filePickControl.setFileAdapterListener(new FileAdapterListener() {
            @Override
            public void onDelete(FileMode fileMode, FileNetworkDeleteListener fileNetworkDeleteListener) {
                //审批过程删除的文件放入数组，提交审批时统一删除
                ModeFormFullFileData formFullFileData =
                        getFileFullMode(context, "form"
                                , fileMode.file
                                , fullModel.serverFullData.launchedForm.formId
                                , fullModel.serverFullData.launchedForm.id
                                , fullModel.formField.getId());
                processDeleteFormFullFileDataList.add(formFullFileData);
            }

            @Override
            public void onAdd(FileMode fileMode, FileNetworkAddListener fileNetworkAddListener) {
                //审批过程添加的文件放入数组，提交审批时统一添加
                ModeFormFullFileData formFullFileData =
                        getFileFullMode(context, "form"
                                , fileMode.file
                                , fullModel.serverFullData.launchedForm.formId
                                , fullModel.serverFullData.launchedForm.id
                                , fullModel.formField.getId());
                processAddFormFullFileDataList.add(formFullFileData);
            }
        });

        //获取提交的文件
        List<ModeFormFullFileData> formFullFileDataList = fullModel.serverFullData.formFileModeMap.get(fullModel.formField.getId());
        if (formFullFileDataList != null) {
            List<FileMode> fileModeList = new ArrayList<>();
            for (ModeFormFullFileData formFullFileData : formFullFileDataList) {
                String fileUrl = COS.getInstance().getFileUrl(formFullFileData.bucket, formFullFileData.path);
                FileMode fileMode = new FileMode();
                fileMode.path = fileUrl;
                fileMode.type = FileMode.NETWORK_FILE;
                fileModeList.add(fileMode);
            }
            //放入提交的文件
            filePickControl.setFileModeList(fileModeList);
        }

        filePickControl.builder();
        fullModel.frameLayout.addView(filePickControl);
        return fullModel.layout;
    }




    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getSelectNumView());     //最大数量
        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印

        return installClass.v;
    }
}
